package com.spark.lola.controller.purchaser;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.spark.lola.entity.sup.AccountInfo;
import com.spark.lola.service.sup.IAccountInfoService;
import com.spark.lola.support.common.util.DataBeanUtils;
import com.spark.lola.support.common.util.ResponseBean;
import com.spark.lola.support.mybatis.controller.BaseController;
import com.spark.lola.support.mybatis.entity.PageParam;
import com.spark.lola.support.mybatis.entity.QueryParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

/**
 * @author: Zhi-peng wang
 * @date: 2020/05/25 16:15
 * 供应商接口
 **/
@RestController
@RequestMapping("/api/pur/account-info")
@Api(value = "/api/pur/account-info", tags = "供应商接口")
public class AccountInfoController extends BaseController {

    @Autowired
    private IAccountInfoService currentService;

    @ApiOperation(value = "分页查询供应商列表")
    @RequestMapping(value = "/findBy", method = RequestMethod.POST)
    public ResponseBean findBy(@RequestBody QueryParam<AccountInfo> queryBean) {
        PageParam<AccountInfo> pageParam = queryBean.toPageParam();
        IPage<AccountInfo> page = currentService.page(pageParam.getPage(), pageParam.getEntityWrapper());
        return ResponseBean.success(page);
    }

    @ApiOperation(value = "查询供应商详情")
    @ApiImplicitParam(name = "id", value = "供应商id", required = true, dataType = "Long", paramType = "path")
    @RequestMapping(value = "/findInfo/{id}", method = RequestMethod.POST)
    public ResponseBean findInfo(@PathVariable Long id) {
        AccountInfo model;
        if (id == 0) {
            model = new AccountInfo();
        } else {
            model = currentService.getById(id);
        }
        return ResponseBean.success(model);
    }

    @ApiOperation(value = "创建/修改供应信息")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @Transactional(rollbackFor = Exception.class)
    public ResponseBean save(@RequestBody AccountInfo form) throws Exception {
        AccountInfo model = new AccountInfo();
        if (form.getId() != null) {
            // 禁止修改项 做空处理
            model = currentService.getById(form.getId());
        }
        DataBeanUtils.buildUpdateBean(model, form);
        currentService.saveOrUpdate(model);
        return ResponseBean.success(model);
    }

}
